<?
ob_start();
include("common/header.html");
include('common/autoload.php');
$out_html = ob_get_contents();
ob_end_clean();
include_once("class/ProOrder.php");
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = SoapInit::GetSoapInside();
$memberID=$_SESSION['sid'];


//查询合并的salerid
if($_SESSION['sdtype']==3){//合并过的终端并且是主体
    $sql = "select preSalerID from pft_terminal_share where nowSalerID={$_SESSION['saccount']}";
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $total_salerid .= $row['preSalerID'].',';
    }
    $total_salerid = rtrim($total_salerid,',');
}else{
    $total_salerid = $_SESSION['account'];
}
//查询所有景区的名字
$sql = "select title,salerid from uu_land where salerid in ($total_salerid)";
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()){
    $ltitle_salerid[$row['salerid']]= $row['title'];
}
$btime=$_REQUEST['begintime']?$_REQUEST['begintime']:date("Y-m-d");
$etime=$_REQUEST['endtime']?$_REQUEST['endtime']:date("Y-m-d");
$by=$_REQUEST['by']?$_REQUEST['by']:"lid";
$option=array();
if(count($_REQUEST['status'])>0){
	$option['status']=implode("|",$_REQUEST['status']);
}
// 门票查询
if(isset($_REQUEST['ticket'])){
	$t = explode("_",$_REQUEST['ticket']);
    $option['tid']=$t[0];
}

//交易时间类型
$timeType = isset($_REQUEST['time_type'])? abs($_REQUEST['time_type']) : 1;
//分销商ID
$fid = isset($_REQUEST['fid']) ? abs($_REQUEST['fid']) : 0;
//TODO::直销

if($_POST['salerid']!=''){//选择了指定的景区供应商
    $salerid = $_POST['salerid'];
}else{
    foreach($ltitle_salerid as $k => $v){
        $salerid .= $k.',';
    }
    $salerid = rtrim($salerid,',');
}
if($_GET['cs']){
    var_dump($salerid);
}
$obj = saleProOrderObject($memberID,$s,$_SESSION['sdtype'], $salerid);
$r = $obj->_orderSumOnLand($btime." 00:00:00",$etime." 23:59:59",$by, $option, $timeType, $fid);
$tickets = $obj->getOrderProducts();
if($_REQUEST['tickets']){ // 门票存在并且多则用获取的
	$tmp = unserialize($_REQUEST['tickets']);
	if(count($tickets)<=count($tmp)) $tickets = $tmp;
}

$titleT=array("lid"=>"景区名称","tid"=>"票类名称","mid"=>"分销商名称");
$totals=array();
include("ExcelHandle.php");/*导出EXCEL处理程序*/
echo $out_html;
$from="sale";
?>
<script src="js/Calendar.js" type="text/javascript"></script>
<script type="text/javascript" src="js/global/Date_func.js"></script>
<link rel="stylesheet" type="text/css" href="css/member.css"/>
<style>
.report{margin:10px 0; padding:15px; border:1px solid #eee}
.report span.part{ margin-right:20px;}
input.time{    background: url("images/rili.png") no-repeat scroll right center #FFFFFF;
border: 1px solid #7F9DB9;
height: 20px;
line-height: 20px;
width: 120px;}
.total{ width:100%; text-align:center;}
input.checkbox{ vertical-align:-2px; margin-right:2px;}
.btn{ background:#eee; padding:4px 15px; *padding:4px 8px; background:url("images/mc_title.png") no-repeat right top; color:#fff; border:none; cursor:pointer}
#excel{margin-left:10px;}
</style>
<h3 class="mc_title">
	<span class="mctit_1 <?=($from!="sale")?"notcur":""?>">
		<a href="orderReport.html"><span class="mctit_2">已卖出产品报表</span></a>
	</span>
	<?if($_a->checkAuth("buyOrderReport.html")){?>
	<span class="mctit_1 <?=($from=="sale")?"notcur":""?>">
		<a href="buyOrderReport.html"><span class="mctit_2">已买到产品报表</span></a>
	</span>
	<?}?>
    <span class="mctit_1 notcur">
		<a href="orderReport_Com.html"><span class="mctit_2">数据罗盘</span></a>
	</span>
</h3>
<form class="report" method="post">
	<input type="hidden" name="tickets" value='<?=serialize($tickets)?>'/>
	<input type="hidden" name="act" id="act" value=""/>
	<p>
		<span class="part">
			<label>
                <!--交易日期：-->
                <select name="time_type" id="time_type">
                    <option value="1" <?=$_REQUEST['time_type']==1?'selected' :''?>>下单时间</option>
                    <option value="2" <?=$_REQUEST['time_type']==2?'selected' :''?>>预计游玩</option>
                    <option value="3" <?=$_REQUEST['time_type']==3?'selected' :''?>>完成时间</option>
                </select>
            </label>
			<input type="text" name="begintime" id="begintime" class="time" value="<?=$btime?>" onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)"/>
			<label>至</label>
			<input type="text" name="endtime" id="endtime" class="time" value="<?=$etime?>" onmouseup="SelectDate(this,'yyyy-MM-dd',0,0)"/>
			<a href="javascript:changeDate('today');" class="sDate">今天</a>
			<a href="javascript:changeDate('yesterday');" class="sDate">昨天</a>
			<a href="javascript:changeDate('week');" class="sDate">本周</a>
			<a href="javascript:changeDate('lastweek');" class="sDate">上周</a>
			<a href="javascript:changeDate('month');" class="sDate">本月</a>
			<a href="javascript:changeDate('lastmonth');" class="sDate">上月</a>
		</span>
		<input type="submit" class="btn" value="查 询"/>
		<input type="button" id="excel" class="btn" value="导 出"/>
	</p>
	<p style="margin:10px 0;">
    <?$_REQUEST['status']=is_array($_REQUEST['status']) ? $_REQUEST['status'] : array();?>
	订单状态：
	<span  class="part">
		<input class="checkbox" type="checkbox" name="status[]" <?=in_array("0|1|2|3|5|6",$_REQUEST['status'])?"checked":""?> value="0|1|2|3|5|6"/><label>全部</label>
		<input class="checkbox" type="checkbox" name="status[]" <?=in_array("0|6",$_REQUEST['status'])?"checked":""?> value="0|6"/><label>未使用</label>
		<input class="checkbox" type="checkbox" name="status[]" <?=(in_array("1",$_REQUEST['status']) ||$_REQUEST['time_type']==3)?"checked":""?> value="1"/><label>已使用</label>
		<input class="checkbox" type="checkbox" name="status[]" <?=(in_array("2",$_REQUEST['status'])&& $_REQUEST['time_type']!=3)?"checked":""?> value="2"/><label>已过期</label>
		<input class="checkbox" type="checkbox" name="status[]" <?=(in_array("3|5",$_REQUEST['status']) && $_REQUEST['time_type']!=3)?"checked":""?> value="3|5"/><label>已取消</label>
	</span>
	<span  class="part">
		统计方式：
		<select name="by">
			<option value="tid" selected >按门票统计</option>
		</select>
	</span>
	</p>
    <p style="margin:10px 0;">
		<span  class="part">
            门票：
            <select name="ticket" id="ticket">
                <option value="">--选择--</option>
				<?
					foreach($tickets as $k=>$ticket){
                        $select = '';
                        if($_POST['ticket'] == $k) $select = 'selected="selected"';
                        echo '<option value="'.$k.'"'.$select.'>'.$ticket.'</option>';
                    }
				?>
            </select>
        </span>
        <span  class="part">
            景区名称:
            <select name="salerid">
                <option value="">选择</option>
                <?foreach($ltitle_salerid as $k => $v){?>
                    <option  value="<?=$k?>" <?if($_POST['salerid']==$k) echo 'selected=selected'?>><?=$v?></option>
                <?}?>
            </select>
        </span>
    </p>
</form>
<div class="rank">
	<div class="rankcon">
		<table class="ranktab">
			<thead id="rankhd">
				<th>序号</th>
				<th class="tL"><?=$titleT[$by]?></th>
				<th><span class="sort" onclick="sortTable('offmain',2, true ,this);">订单数量</span></th>
				<th><span class="sort" onclick="sortTable('offmain',3, true ,this);">门票数量</span></th>
				<th><span class="sort" onclick="sortTable('offmain',4, true ,this);">金额</span></th>
				<th><span class="sort" onclick="sortTable('offmain',5, true ,this);">余额支付</span></th>
				<th><span class="sort" onclick="sortTable('offmain',6, true ,this);">支付宝</span></th>
				<th><span class="sort" onclick="sortTable('offmain',7, true ,this);">授信支付</span></th>
				<th><span class="sort" onclick="sortTable('offmain',8, true ,this);">产品自销</span></th>
				<th><span class="sort" onclick="sortTable('offmain',9, true ,this);">现场支付</span></th>
                <th><span class="sort" onclick="sortTable('offmain',10, true ,this);">微信</span></th>
                <th><span class="sort" onclick="sortTable('offmain',11, true ,this);">银联</span></th>
                <th><span class="sort" onclick="sortTable('offmain',12, true ,this);">环迅</span></th>
			</thead>
			<tbody id="offmain">
				<?foreach($r as $lid=>$row){
					$totals['order']+=$row['order'];
					$totals['tnum']+=$row['tnum'];
					$totals['money']+=$row['money']/100;
					$totals['pmode0']+=$row['pmode0']/100;
					$totals['pmode1']+=$row['pmode1']/100;
					$totals['pmode2']+=$row['pmode2']/100;
					$totals['pmode3']+=$row['pmode3']/100;
					$totals['pmode4']+=$row['pmode4']/100;
                    $totals['pmode5']+=$row['pmode5']/100;
                    $totals['pmode7']+=$row['pmode7']/100;
                    $totals['pmode8']+=$row['pmode8']/100;
				?>
				<tr>
					<td><?=++$n?></td>
					<td class="tL"><?=$row['title']?></td>
					<td><?=$row['order']?></td>
					<td><?=$row['tnum']?></td>
					<td><?=$row['money']/100?></td>
					<td><?=$row['pmode0']/100?></td>
					<td><?=$row['pmode1']/100?></td>
					<td><?=$row['pmode2']/100?></td>
					<td><?=$row['pmode3']/100?></td>
					<td><?=$row['pmode4']/100?></td>
                    <td><?=$row['pmode5']/100?></td>
                    <td><?=$row['pmode7']/100?></td>
                    <td><?=$row['pmode8']/100?></td>
				</tr>
				<?}?>
			</tbody>
			<tfoot>
			<?if(count($r)>0){?>
				<tr>
					<td  colspan="2">总计：</td>
					<td><?=$totals['order']?></td>
					<td><?=$totals['tnum']?></td>
					<td><?=$totals['money']?></td>
					<td><?=$totals['pmode0']?></td>
					<td><?=$totals['pmode1']?></td>
					<td><?=$totals['pmode2']?></td>
					<td><?=$totals['pmode3']?></td>
					<td><?=$totals['pmode4']?></td>
                    <td><?=$totals['pmode5']?></td>
                    <td><?=$totals['pmode7']?></td>
                    <td><?=$totals['pmode8']?></td>
				</tr>
			<?}else{?>
				<tr>
					<td align="left" colspan="9">&nbsp;&nbsp;&nbsp;交易日期无销售订单！</td>
				</tr>
			<?}?>
			</tfoot>
		</table>
	</div>
</div>
<script src="js/jsorder.js"></script>
<script src="js/jsorder.js"></script>
<script>
$("#time").change(function(){
    Date_func.selectDate($(this).val(),$("#begintime").get(0),$("#endtime").get(0));
});
$("input.btn").click(function(){
    var btime=$("#begintime").val();
    var etime=$("#endtime").val();
    btime=btime.split("-");
    etime=etime.split("-");
    month=(etime[0]-btime[0])*12+(etime[1]-btime[1]);
    if(month>3){
        alert("交易日期时间间隔不能超过三个月");
        return false;
    }
});
// 获取门票
$("#land").change(function(){
    
    landid=$(this).val();
    if(landid==''){
        _options = '<option value="">--选择--</option>';
        $("#ticket").html(_options);
        return;
    }
    $.ajax({
        uri:window.location.href,
        data:"landid="+landid,
        beforeSend:function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("request_type","getTicket");
        },
        success: function(data){
            data = JSON.parse(data);
            if(data.s=='success'){
                _options = '<option value="">--选择--</option>';
                for(var i in data.content){
                    _options+='<option value="'+data.content[i].id+'_'+data.content[i].title+'">'+data.content[i].title+'</option>';
                }
                $("#ticket").html(_options);

            }
        }
    });
});

$("#excel").click(function(){
    $("#act").val("orderReportExcel");
    $("form.report").submit();
    $("#act").val("");
});

function changeDate(date){
    // lastweek颠倒了
    if(date=='lastweek'){
        Date_func.selectDate(date,$("#endtime").get(0),$("#begintime").get(0));
    }else{
        Date_func.selectDate(date,$("#begintime").get(0),$("#endtime").get(0));
    }
}
</script>
<?include("common/footer.html");?>